package com.yb.flowable.controller;

import com.alibaba.fastjson.JSONObject;
import com.yb.flowable.common.LoginUser;
import com.yb.flowable.common.LoginUserUtils;
import com.yb.flowable.common.Result;
import com.yb.flowable.param.Vacation;
import com.yb.flowable.param.VacationInfo;
import com.yb.flowable.param.VacationTask;
import com.yb.flowable.service.MyFlowableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.flowable.idm.api.User;
import org.hibernate.validator.constraints.Length;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * Description:请假流程接口类
 * 运行常用表act_ru_variable/act_ru_task/act_ru_execution/act_ru_identitylink/act_ru_actinst
 * 储存常用表act_re_procdef/act_re_deployment
 * 历史常用表act_hi_procinst/act_hi_taskinst/act_hi_varinst/act_hi_identitylink/act_hi_comment/act_hi_actinst
 *
 * @author biaoyang
 * date 2019/3/12 001217:58
 */
@Api(tags = "请假流程")
@Validated
@RestController
@AllArgsConstructor
public class MyFlowableController {
    private MyFlowableService myFlowableService;

    @ApiOperation("填写请假信息开始请假")
    @PostMapping("/startVacation")
    public Result<Object> startVacation(@Valid @RequestBody VacationInfo vacation) {
        String result = myFlowableService.startVacation(vacation);
        return Result.success(result);
    }

    @ApiOperation("查询我的请假信息")
    @GetMapping("/queryMyVacation")
    public Result<Object> queryMyVacation() {
        List<Vacation> result = myFlowableService.queryMyVacation();
        return Result.success(result);
    }

    @ApiOperation("查询我的请假记录")
    @GetMapping("/myVacationRecord")
    public Result<Object> myVacationRecord() {
        List<Vacation> result = myFlowableService.myVacationRecord();
        return Result.success(result);
    }

    @ApiOperation("请假流程审批人信息查询")
    @GetMapping("/queryAuditorInfo")
    public Result<Object> queryAuditorInfo() {
        List<JSONObject> result = myFlowableService.queryAuditorInfo();
        return Result.success(result);
    }

    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    @ApiOperation("查询我的审批任务信息")
    @GetMapping("/queryMyAuditInfo")
    public Result<Object> queryMyAuditInfo() {
        List<VacationTask> result = myFlowableService.queryMyAuditInfo();
        return Result.success(result);
    }

    @ApiOperation("任务审批")
    @PostMapping("/auditTask")
    public Result<Object> auditTask(
            @ApiParam("是否批准(true-批准false-不批准)")
            @NotNull(message = "是否批准不能为空")
            @RequestParam Boolean isAudit) {
        String result = myFlowableService.auditTask(isAudit);
        return Result.success(result);
    }

    @ApiOperation("查询我的审批记录")
    @GetMapping("/queryMyAuditRecord")
    public Result<List<Vacation>> queryMyAuditRecord() {
        List<Vacation> result = myFlowableService.queryMyAuditRecord();
        return Result.success(result);
    }

    //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

    @ApiOperation("下载(查看)流程图(PNG),未被标红的是没有开始的流程")
    @GetMapping(value = "/generateProcessDiagram", produces = MediaType.IMAGE_PNG_VALUE)
    public void generateProcessDiagram(
            HttpServletResponse response,
            @ApiParam("流程实例id")
            @Length(max = 100, message = "流程实例id有误")
            @NotBlank(message = "流程实例id不能为空")
            @RequestParam String processInstanceId) {
        myFlowableService.generateProcessDiagram(response, processInstanceId);
    }

}
